#include"Bag.h"
#include"Item.h"

#include<vector>
#include<string>
#include<iomanip>
#include <iostream>

using namespace std;

int BackPack::getCurrentCount()const {
	return  currentNum;
}

void BackPack::showInfo() {
	cout << "背包容量：" << getMaxCapacity() << endl;
	cout << "当前物品数量：" << getCurrentCount() << endl;
}

int BackPack::getMaxCapacity()const {
	return maxCapacity;
}

BackPack::BackPack(int maxCapacity) :
	maxCapacity(maxCapacity), currentNum(0) {
}

bool BackPack::isEmpty()const {
	return ItemList.empty();
}

bool BackPack::isFull()const {
	return(currentNum >= maxCapacity);
}

bool BackPack::addItem(Item* item) {
	if (this->isFull()) {
		cout << "【背包物品已满】无法添加!" << item->getName() <<
			"请先丢弃物品" << endl;
		return false;
	}
	if (item == nullptr) {
		cout << "【错误】无效添加!" << endl;
		return false;
	}
	currentNum++;
	ItemList.push_back(item);
	/*cout << "成功添加物品：" << item->getName() << "已加入背包，当前背包容量" <<
		getCurrentCount() << "/" << getMaxCapacity() << endl;*/
	return true;
}

void BackPack::removeItem(Item* item) {
	if (item == nullptr) {
		cout << "【错误】无法删除空物品" << endl;
		return;
	}
	for (auto it = ItemList.begin(); it != ItemList.end(); it++) {
		if (*it == item) {
			ItemList.erase(it);
			currentNum--;
			cout << "成功移除物品：" << item->getName() << "当前背包容量" <<
				getCurrentCount() << "/" << getMaxCapacity() << endl;
			return;
		}
	}
	cout << "背包中没有该物品" << endl;
	return;
}

void BackPack::removeItemByIndex(int index) {
	if (index < 0 || index >= ItemList.size()) {
		cout << "无效索引" << endl;
		return;
	}
	Item* item = ItemList[index];
	removeItem(item);
	return;
}

void BackPack::clearBackPack() {
	if (isEmpty()) {
		cout << "背包已为空，无需清空" << endl;
		return;
	}
	cout << "是否清空背包？（Y/N）" << endl;
	char type;
	cin >> type;
	if (type != 'Y' && type != 'y') {
		cout << "已取消清空操作" << endl;
		return;
	}
	ItemList.clear();
	currentNum = 0;
	cout << "背包已清空" << endl;
	return;
}

// 新增：展示物品列表（无交互功能）
void BackPack::displayItems() const {
	if (this->isEmpty()) {
		std::cout << "背包中没有物品。" << endl;
		return;
	}

	cout << "====================背包内容 ====================" << endl;
	cout << "背包容量：" << currentNum << "/" << maxCapacity << endl;
	cout << left << setw(10) << "索引" << setw(15) << "物品名称" << setw(12) <<
		"物品类型" << setw(10) << "价值" << endl;

	for (int i = 0; i < (int)ItemList.size(); ++i) {
		const Item* item = ItemList[i];
		string itemType;

		if (dynamic_cast<const Equipment*>(item) != nullptr) {
			itemType = "装备";
		}
		else if (dynamic_cast<const Drug*>(item) != nullptr) {
			itemType = "药水";
		}
		else if (dynamic_cast<const Special*>(item) != nullptr) {
			itemType = "特殊物品";
		}
		else {
			itemType = "未知物品";
		}

		cout << left << setw(10) << i << setw(15) << item->getName()
			<< setw(12) << itemType << setw(10) << item->getValue() << endl;
	}
	cout << "============================================================" << endl;
}

void BackPack::showBag(Player& player) {
	if (this->isEmpty()) {
		std::cout << "背包中没有物品。" << endl;
		return;
	}
	int choice = -1;
	while (choice != 0) {
		cout << "====================背包内容 ====================" << endl;
		cout << "背包容量：" << currentNum << "/" << maxCapacity << endl;
		cout << left << setw(10) << "索引" << setw(15) << "物品名称" << setw(12) <<
			"物品类型" << setw(10) << "价值" << endl;
		for (int i = 0; i < (int)ItemList.size(); ++i) {
			Item* item = ItemList[i];
			string itemType;
			if (dynamic_cast<Equipment*>(item) != nullptr) {
				itemType = "装备";
			}
			else if (dynamic_cast<Drug*>(item) != nullptr) {
				itemType = "药水";
			}
			else if (dynamic_cast<Special*>(item) != nullptr) {
				itemType = "特殊物品";
			}
			else {
				itemType = "未知物品";
			}
			cout << left << setw(10) << i << setw(15) << item->getName()
				<< setw(12) << itemType << setw(10) << item->getValue() << endl;
		}
		cout << "============================================================" << endl;
		cout << "请输入操作:" << endl;
		cout << "1.输入物品索引查看详细信息|0.返回" << endl;
		cout << "请输入：" << endl;
		if (!(cin >> choice)) {
			cout << "输入错误，请输入数字！" << endl;
			cin.clear();  // 清除错误状态
			char c;
			while (cin.get(c) && c != '\n');  // 清空输入缓冲区
			choice = -1;
			system("pause");
			continue;
		}
		if (choice == 1) {
			cout << "请输入索引:";
			int index;
			if (!(cin >> index)) {
				cout << "输入错误，请输入数字！" << endl;
				cin.clear();
				char c;
				while (cin.get(c) && c != '\n');
				system("pause");
				continue;
			}

			if (index < 0 || index >= ItemList.size()) {
				cout << "无效索引！" << endl;
				cin.clear();
				char c;
				while (cin.get(c) && c != '\n');
				system("pause");
				continue;
			}

			Item* item = getItemByIndex(index);
			if (!item) {
				system("pause");
				continue;
			}

			item->showInfo();

			cout << "是否使用物品？(Y/N)";
			char temp;
			cin >> temp;

			if (temp == 'Y' || temp == 'y') {
				useItemByIndex(index, player);
				removeItemByIndex(index);
				system("pause");
			}
			else if (temp == 'N' || temp == 'n') {  // 正确处理不使用的情况
				// 不做操作，继续循环
			}
			else {
				cout << "无效操作。" << endl;
				system("pause");
			}
		}
		else if (choice != 0) {  // 移除此处多余的花括号
			cout << "无效操作，请重新输入！" << endl;
			system("pause");
		}
	}

}

void BackPack::equipItem(Equipment* equip, BackPack& backpack) {
	if (equip == nullptr) {
		return;
	}
	EquipSlot slot = equip->getSlot();
	string slotName = equip->slotToString(slot);

	if (equip->hasEquipment(slot)) {
		Equipment* oldEquip = equip->equippedItems[slot];
		cout << "卸下了" << oldEquip->getName() << endl;

		if (!backpack.addItem(oldEquip)) {
			cout << "背包已满，旧装备:" << oldEquip->getName() << "已丢失！" << endl;
			delete oldEquip;
		}
	}
	equip->equippedItems[slot] = equip;
	cout << "成功装备" << equip->getName() << "到" << slotName << "槽" << endl;
}

void BackPack::useItemByIndex(int index, Player& player) {
	if (index < 0 || index >= ItemList.size()) {
		return;
	}
	Item* item = ItemList[index];
	item->showInfo();
	if (Equipment* equip = dynamic_cast<Equipment*>(item)) {
		EquipSlot slot = equip->getSlot();
		string part = equip->slotToString(slot);
		if (equip->hasEquipment(slot)) {
			cout << "该部位(" << part << ")已装备物品，是否替换？（Y/N）";
			char confirm;
			cin >> confirm;
			if (confirm != 'Y' || confirm != 'y') {
				cout << "已取消装备" << endl;
				return;
			}
			else if (confirm != 'N' || confirm != 'n') {
				cout << "【错误】无效输入" << endl;
				return;
			}
			Equipment* oldEquip = equip->unEquipPart(slot);
			if (oldEquip) {
				oldEquip->showInfo();
				if (!isFull()) {
					addItem(oldEquip);
					cout << "原有" << equip->slotToString(slot) << "已放回背包" << endl;
				}
				else {
					cout << "背包已满，原有" << equip->slotToString(slot) << "已丢失！" << endl;
					delete oldEquip;
				}
			}
		}
		equipItem(equip, *this);
		cout << "成功装备" << equip->getName() << endl;
		return;
	}
	else if (Special* special = dynamic_cast<Special*>(item)) {
		bool isConsumed = special->use();
		return;
	}
	cout << "无法使用物品！" << endl;
	return;
}
